# sklearn 调用 LinearRegression 类

import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

X1 = 2 * np.random.rand(100, 1)
X2 = 2 * np.random.rand(100, 1)
Y = 4 + 3 * X1 + 5 * X2 + np.random.rand(100, 1)
# 有截距
sk_linear = LinearRegression()
X_c = np.c_[X1, X2]
sk_linear.fit(X_c, Y)
print('截距:%s , 参数系数:%s' % (sk_linear.intercept_, sk_linear.coef_))

# X_new = np.random.uniform(low=0, high=4, size=(100, 2))
X_new = np.array([[0, 0], [2, 1], [2, 4]])
y_predict = sk_linear.predict(X_new)

# 无截距
lin_reg = LinearRegression(fit_intercept=False)
lin_reg.fit(X_c, Y)
print('截距:%s , 参数系数:%s' % (lin_reg.intercept_, lin_reg.coef_))

plt.plot(X_new, sk_linear.predict(X_new), 'r-')
plt.plot(X_new, lin_reg.predict(X_new), 'g--')
plt.plot(X_c, Y, 'g.')
plt.axis([0, 2.5, 0, 50])

plt.show()
